package com.acc.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.acc.entity.EasyUIDataGridJSON;
import com.acc.entity.Msg;
import com.acc.entity.Order;
import com.acc.entity.Standby;
import com.acc.entity.User;
import com.acc.service.OrderService;
import com.acc.service.impl.OrderServiceImpl;
import com.acc.utils.Generate_orderId;
import com.alibaba.fastjson.JSON;


/**
 * @author 姜伟康
 *2017-9-12
 */

public class OrderController extends InnerDispacherController {

	 OrderService orderService =new OrderServiceImpl();

	 boolean result = false ;
		
	 
	//显示所有订单
	public void showAllOrder(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {
        
		//从session中获取用户信息
	           HttpSession httpSession = request.getSession();
	           User user = (User) httpSession.getAttribute( "user");
		//获取用户姓名
		 String username  = user.getUserName();
		//通过姓名查找其所有车票
		Integer page = Integer.valueOf(request.getParameter("page"));
		Integer rows = Integer.valueOf(request.getParameter("rows"));
	    int num = orderService.selectOrderNum(username); 
		List<Order> orders = orderService.showAllOrder(username,page*rows,(page-1)*rows);
		
		EasyUIDataGridJSON responseObject = new EasyUIDataGridJSON();
		responseObject.setTotal(num);
		responseObject.setRows(orders);
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		response.getWriter().append(JSON.toJSONString(responseObject));
	}

	
	//新增订单
	public void addOrder(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {
		
        //获取session中的用户
	     HttpSession httpSession = request.getSession();
         User user = (User) httpSession.getAttribute( "user");
 		//获取用户姓名
		 String username  = user.getUserName();
		//创建订单号
		String orderId = Generate_orderId.create_OrederId();
		//获取选中的车次
	    int standbyid = Integer.parseInt(request.getParameter("id"));
		//执行新增订单
		result = orderService.addOrder( orderId ,username, standbyid);
		//转发到成功提示界面
		if (result) {
			Msg msg = new Msg();
			msg.setSuccess(true);
			msg.setErrorMsg(null);
			response.getWriter().append(JSON.toJSONString(msg));
			
		}
	}
	
	
	//支付订单
	public void payOrder(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {
		
		//获取选中的订单号
		String orderId =request.getParameter("orderId");
		//执行支付订单
		result = orderService.payOrder( orderId) ;
		//转发到成功提示界面
		if (result) {
			Msg msg = new Msg();
			msg.setSuccess(true);
			msg.setErrorMsg(null);
			response.getWriter().append(JSON.toJSONString(msg));
			
		}
	}
	
	
	//取消订单
	public void deleteOrder(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {

		//获取选中的订单号
		String orderId = request.getParameter("orderId");
		//执行取消订单
		result = orderService.deleteOrder(orderId);
		//转发到成功提示界面
		if (result) {
			Msg msg = new Msg();
			msg.setSuccess(true);
			msg.setErrorMsg(null);
			response.getWriter().append(JSON.toJSONString(msg));
			
		}
	}
	
	//改签
	public void updateOrder(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {
		
		//获取选中的车票
		Standby standby = (Standby) request.getAttribute("standby");
		//获取选中的订单号
		String orderId = (String) request.getAttribute("orderId");
		//执行改签
		result = orderService.updateOrder( orderId,standby);
		//转发到成功提示界面
		if (result) {
			Msg msg = new Msg();
			msg.setSuccess(true);
			msg.setErrorMsg(null);
			response.getWriter().append(JSON.toJSONString(msg));
			
		}
	}
	
	//退票
	public void returnOrder(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {

		//获取选中的订单号
		String orderId = request.getParameter("orderId");
		//执行退票
		result = orderService. returnOrder( orderId) ;
		//转发到成功提示界面
		if (result) {
			Msg msg = new Msg();
			msg.setSuccess(true);
			msg.setErrorMsg(null);
			response.getWriter().append(JSON.toJSONString(msg));
			
		}
	}
	
	//取票
	public void takingOrder(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {
		
		//获取选中的订单号
		String orderId = request.getParameter("orderId");
		//执行取票
		result = orderService.takingOrder( orderId) ;
		//转发到成功提示界面
		if (result) {
			Msg msg = new Msg();
			msg.setSuccess(true);
			msg.setErrorMsg(null);
			response.getWriter().append(JSON.toJSONString(msg));
			
		}

	}
	

	

}
